package com.dlwx.firefighting.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.inter.PayPswInter;
import com.zhy.autolayout.AutoLinearLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/12.
 */

public class PayPswDialog extends BottomBaseDialog {
    @BindView(R.id.close_tv)
    TextView closeTv;
    @BindView(R.id.tv_psw_one)
    TextView tvPswOne;
    @BindView(R.id.tv_psw_two)
    TextView tvPswTwo;
    @BindView(R.id.tv_psw_three)
    TextView tvPswThree;
    @BindView(R.id.tv_psw_four)
    TextView tvPswFour;
    @BindView(R.id.tv_psw_five)
    TextView tvPswFive;
    @BindView(R.id.tv_psw_six)
    TextView tvPswSix;
    @BindView(R.id.tv_psw_note)
    TextView tvPswNote;
    @BindView(R.id.tv_counter_one)
    TextView tvCounterOne;
    @BindView(R.id.tv_counter_two)
    TextView tvCounterTwo;
    @BindView(R.id.tv_counter_three)
    TextView tvCounterThree;
    @BindView(R.id.tv_counter_four)
    TextView tvCounterFour;
    @BindView(R.id.tv_counter_five)
    TextView tvCounterFive;
    @BindView(R.id.tv_counter_six)
    TextView tvCounterSix;
    @BindView(R.id.tv_counter_seven)
    TextView tvCounterSeven;
    @BindView(R.id.tv_counter_eight)
    TextView tvCounterEight;
    @BindView(R.id.tv_counter_nine)
    TextView tvCounterNine;
    @BindView(R.id.tv_counter_point)
    TextView tvCounterPoint;
    @BindView(R.id.tv_counter_zero)
    TextView tvCounterZero;
    @BindView(R.id.ll_money_cancel)
    AutoLinearLayout llMoneyCancel;
    @BindView(R.id.tv_counter_sure)
    TextView tvCounterSure;
    private Context context;
    private PayPswInter inter;

    public PayPswDialog(Context context,PayPswInter inter) {
        super(context);
        this.context = context;
        this.inter=inter;
    }

    @Override
    public View onCreateView() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_pay_psw, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void setUiBeforShow() {

    }

    @OnClick({R.id.close_tv, R.id.tv_psw_one, R.id.tv_psw_two, R.id.tv_psw_three, R.id.tv_psw_four, R.id.tv_psw_five, R.id.tv_psw_six, R.id.tv_counter_one, R.id.tv_counter_two, R.id.tv_counter_three, R.id.tv_counter_four, R.id.tv_counter_five, R.id.tv_counter_six, R.id.tv_counter_seven, R.id.tv_counter_eight, R.id.tv_counter_nine, R.id.tv_counter_point, R.id.tv_counter_zero, R.id.ll_money_cancel, R.id.tv_counter_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_tv:
                this.dismiss();
                break;
            case R.id.tv_psw_one:
                break;
            case R.id.tv_psw_two:
                break;
            case R.id.tv_psw_three:
                break;
            case R.id.tv_psw_four:
                break;
            case R.id.tv_psw_five:
                break;
            case R.id.tv_psw_six:
                break;
            case R.id.tv_counter_one:
                inputPSW(1);
                break;
            case R.id.tv_counter_two:
                inputPSW(2);
                break;
            case R.id.tv_counter_three:
                inputPSW(3);
                break;
            case R.id.tv_counter_four:
                inputPSW(4);
                break;
            case R.id.tv_counter_five:
                inputPSW(5);
                break;
            case R.id.tv_counter_six:
                inputPSW(6);
                break;
            case R.id.tv_counter_seven:
                inputPSW(7);
                break;
            case R.id.tv_counter_eight:
                inputPSW(8);
                break;
            case R.id.tv_counter_nine:
                inputPSW(9);
                break;
            case R.id.tv_counter_zero:
                inputPSW(0);
                break;
            case R.id.ll_money_cancel:
                if (curPosi <= 1) {
                    ToastUtils.toastForShort(context, "请先输入");
                } else {
                    cancelPSW();
                }
                break;
            case R.id.tv_counter_sure:
                String withdrawPSW = strBuilder.toString();
                inter.getPsw(withdrawPSW);
                this.dismiss();
//              ToastUtils.toastForShort(context,withdrawPSW);
                break;
        }
    }

    /**
     * 清空密码
     */
    private void clearPSW() {
        tvPswOne.setText("");
        tvPswTwo.setText("");
        tvPswThree.setText("");
        tvPswFour.setText("");
        tvPswFive.setText("");
        tvPswSix.setText("");
        //清空stringbuilder
        strBuilder.delete(0, strBuilder.length());
        curPosi = 1;
    }

    /**
     * 根据位置清除密码
     */
    private void cancelPSW() {
        switch (curPosi) {
            case 7:
                tvPswSix.setText("");
                strBuilder.deleteCharAt(5);
                break;
            case 6:
                tvPswFive.setText("");
                strBuilder.deleteCharAt(4);
                break;
            case 5:
                tvPswFour.setText("");
                strBuilder.deleteCharAt(3);
                break;
            case 4:
                tvPswThree.setText("");
                strBuilder.deleteCharAt(2);
                break;
            case 3:
                tvPswTwo.setText("");
                strBuilder.deleteCharAt(1);
                break;
            case 2:
                tvPswOne.setText("");
                strBuilder.deleteCharAt(0);
                break;
        }
        curPosi--;
    }

    private int curPosi = 1;
    private StringBuilder strBuilder = new StringBuilder();


    /**
     * 输入提现密码
     */
    private void inputPSW(int num) {
        String strNum = String.valueOf(num);
        switch (curPosi) {
            case 1:
                tvPswOne.setText(strNum);
                strBuilder.append(strNum);
                curPosi++;
                break;
            case 2:
                tvPswTwo.setText(strNum);
                strBuilder.append(strNum);
                curPosi++;
                tvPswOne.setText("*");
                break;
            case 3:
                tvPswThree.setText(strNum);
                strBuilder.append(strNum);
                curPosi++;
                tvPswTwo.setText("*");
                break;
            case 4:
                tvPswFour.setText(strNum);
                strBuilder.append(strNum);
                curPosi++;
                tvPswThree.setText("*");
                break;
            case 5:
                tvPswFive.setText(strNum);
                strBuilder.append(strNum);
                curPosi++;
                tvPswFour.setText("*");
                break;
            case 6:
                tvPswSix.setText(strNum);
                strBuilder.append(strNum);
                curPosi++;
                tvPswFive.setText("*");
                tvPswSix.setText("*");
                break;
            default:
                tvPswSix.setText("*");
                ToastUtils.toastForShort(context, "最多输入6位密码");
                curPosi = 7;
                break;
        }

    }
}
